package com.example.myapplication1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {
    private ImageView headPic;
    private EditText username;
    private EditText password;
    private Button login;
    private TextView regi;
    public MyDatabaseHelper dbHelper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        dbHelper = new MyDatabaseHelper(this, "Userinfo.db", null, 2);

        headPic = (ImageView) findViewById(R.id.head_pic);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.login);
        regi = (TextView) findViewById(R.id.regi);
        regi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);

            }
        });
        password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                String u =username.getText().toString();

                if(!u.equals("")){
                    SharedPreferences sp = getSharedPreferences("userinfo",MODE_PRIVATE);

                }
            }
        });

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                Cursor cursor = db.query("userinfo", null, null, null, null, null, null);
                String u = username.getText().toString();
                String p = password.getText().toString();
                int y =0;
                int x = 0;
                if (cursor.moveToFirst()) {
                     y = 1;
                    do {
                        String username = cursor.getString(cursor.
                                getColumnIndex("username"));
                        String password = cursor.getString(cursor.
                                getColumnIndex("password"));
                        if(u.equals("")||p.equals("")){
                            Toast.makeText(LoginActivity.this, "请输入用户名或密码", Toast.LENGTH_SHORT).show();
                            break;
                        }else{
                            if(u.equals(username) && p.equals(password)){
                                Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(LoginActivity.this, MainPageNewsActivity.class);
                                startActivity(intent);
                                x=0;
                                break;
                            }else{
                                x=1;
                            }
                        }

                    } while (cursor.moveToNext());
                }
                cursor.close();

                if(x==1){

                    Toast.makeText(LoginActivity.this, "登录失败，请检查用户名和密码是否正确", Toast.LENGTH_SHORT).show();

                }
                if(y==0){
                    Toast.makeText(LoginActivity.this, "数据库没有用户，请注册账号", Toast.LENGTH_SHORT).show();


                }




            }
        });
    }
}